package com.crm.controller.reportsController;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.OpportunityDAO;
import com.crm.dao.QuotationDAO;
import com.crm.dao.UserDAO;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.entity.Quotation;
import com.crm.entity.User;

@Controller
@RequestMapping("/funnel")
public class FunnelController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping("/show.do")
	public ModelAndView show(ModelAndView model,HttpSession session,HttpServletRequest request,HttpServletResponse response){
		String userid="info@kkcircuits.us";
		String pass="kkce123";
		String exeName=request.getParameter("exeName");
		UserDAO UDao= new UserDAO();
		logger.info("in x0029 searching  CompanyUserid " + userid + " pass  "
				+ pass);
		try {
			
			 User u=UDao.validatCredentials(userid,pass);
			 logger.info("BusinessUser is "+u);
			 if(u!=null && u.getEmail().trim().length()>0){
				logger.info("session is "+session.getAttribute("loginCompanyUser"));
				Business business=null;
				try {
					
					business=u.getBusiness();
					System.out.println("company id for User is "+business.getBusinessName());
					//session.setAttribute("business",business);
					//session.setAttribute("user",u);
				} catch (Exception e) {
					e.printStackTrace();
				}	
				//OpportunityDAO opdao=new OpportunityDAO();
				List<Opportunity> oplist=new ArrayList<Opportunity>();
				oplist=OpportunityDAO.getSingleInstance().getEnquiry(business.getBusinessId(),exeName);
				System.out.println("oplist "+oplist);
				
				QuotationDAO qtdao=new QuotationDAO();
				List<Quotation> qtlist=new ArrayList<Quotation>();
				for(int i=0;i<oplist.size();i++){
					List<Quotation> qlist=new ArrayList<Quotation>();
					qlist=qtdao.getQuotaion(oplist.get(i).getId());
					
					for(int j=0;j<qlist.size();j++){
						System.out.println("qlist in controller "+qlist.get(j).getId());
						qtlist.add(qlist.get(j));
					}
				}
				
				
				String fileName = "FunnelReport.xls";
				response.setHeader("Content-Disposition", "inline; filename=" + fileName);
				
				System.out.println("sirdhar");
				model.setViewName("/jsp/funnelReport.jsp");
				model.addObject("qtlist", qtlist);
				return model;
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return model;
	}

}
